/******************************************************************************* 
 * Copyright (c) 2008 xored software, Inc.  
 * 
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0 
 * which accompanies this distribution, and is available at 
 * http://www.eclipse.org/legal/epl-v10.html  
 * 
 * Contributors: 
 *     xored software, Inc. - initial API and Implementation (Yuri Strot) 
 *******************************************************************************/
package com.xored.glance.internal.ui.panels;

import java.util.List;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.PopupDialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * @author Yuri Strot
 * 
 */
public class SearchDialog extends PopupDialog {

	public SearchDialog(Shell parent) {
		super(parent, SWT.RESIZE, true, false, false, true, false, null, null);
	}

	@Override
	protected Control createContents(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		POPUP_LAYOUT_FACTORY.applyTo(composite);
		LAYOUTDATA_GRAB_BOTH.applyTo(composite);

		titleArea = (Composite) createTitleMenuArea(composite);
		separator = createHorizontalSeparator(composite);
		createInfoTextArea(composite);

		applyColors(composite);
		applyFonts(composite);
		return composite;
	}

	protected Control createInfoTextArea(Composite parent) {
		// Status label
		info = new Label(parent, SWT.RIGHT);
		info.setText(HELP_TEXT);

		GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL,
				SWT.BEGINNING).applyTo(info);
		info.setForeground(parent.getDisplay().getSystemColor(
				SWT.COLOR_WIDGET_DARK_SHADOW));
		return info;
	}

	@Override
	protected void setInfoText(String text) {
		info.setText(text);
	}

	protected void applyColors(Composite composite) {
		applyForegroundColor(getForeground(), composite);
		applyBackgroundColor(getBackground(), composite);
	}

	protected void applyFonts(Composite composite) {
		Dialog.applyDialogFont(composite);

		if (info != null) {
			Font font = info.getFont();
			FontData[] fontDatas = font.getFontData();
			for (int i = 0; i < fontDatas.length; i++) {
				fontDatas[i].setHeight(fontDatas[i].getHeight() * 9 / 10);
			}
			infoFont = new Font(info.getDisplay(), fontDatas);
			info.setFont(infoFont);
		}
	}

	@Override
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.addDisposeListener(new DisposeListener() {

			public void widgetDisposed(DisposeEvent e) {
				handleClose();
			}
		});
	}

	protected void handleClose() {
		if (infoFont != null && !infoFont.isDisposed()) {
			infoFont.dispose();
		}
		infoFont = null;
	}

	/**
	 * Create a horizontal separator for the given parent.
	 * 
	 * @param parent
	 *            The parent composite.
	 * @return The Control representing the horizontal separator.
	 */
	private Control createHorizontalSeparator(Composite parent) {
		Label separator = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL
				| SWT.LINE_DOT);
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true,
				false).applyTo(separator);
		return separator;
	}

	@SuppressWarnings("unchecked")
	protected List getForegroundColorExclusions() {
		List list = super.getForegroundColorExclusions();
		if (info != null)
			list.add(info);
		if (separator != null)
			list.add(separator);
		return list;
	}

	@SuppressWarnings("unchecked")
	protected List getBackgroundColorExclusions() {
		List list = super.getBackgroundColorExclusions();
		if (separator != null)
			list.add(separator);
		return list;
	}

	protected void applyBackgroundColor(Color color) {
		applyBackgroundColor(color, titleArea);
	}

	private Composite titleArea;

	protected Text titleText;
	private Font infoFont;
	private Label info;
	private Control separator;

	private static final GridDataFactory LAYOUTDATA_GRAB_BOTH = GridDataFactory
			.fillDefaults().grab(true, true);
	private static final GridLayoutFactory POPUP_LAYOUT_FACTORY = GridLayoutFactory
			.fillDefaults().margins(POPUP_MARGINWIDTH, POPUP_MARGINHEIGHT)
			.spacing(POPUP_HORIZONTALSPACING, POPUP_VERTICALSPACING);

	protected static final String HELP_TEXT = "Enter search text";

}
